package com.kewo.autioscrollanimviewpagerdemo;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by Administrator on 2016/7/26.
 */
public class MyAdadapter extends PagerAdapter {

    List<View> viewList;

    public MyAdadapter() {
    }

    public MyAdadapter(List<View> viewList) {
        this.viewList = viewList;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    //验证ViewPager是否正常工作
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public Object instantiateItem(ViewGroup container,  int position) {

//        //对ViewPager页号求模取出View列表中要显示的项
        position %= viewList.size();
        if (position<0){
            position = viewList.size()+position;
        }
        ImageView view = (ImageView) viewList.get(position);
        //如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
        ViewParent vp =view.getParent();
        if (vp!=null){
            ViewGroup parent = (ViewGroup)vp;
            parent.removeView(view);
        }
        container.addView(view);
        //add listeners here if necessary
        return view;
//        ViewPager vp = (ViewPager)container;
//        if (vp.getChildCount()>0) {
//            vp.removeView(viewList.get(position % (viewList.size() - 1)));
//        }

//        container.addView(viewList.get(position%(viewList.size()-1)));
//
//
//        //使用系统VIewPager正常，如果是自定义ViewPager点击失效
//        viewList.get(position).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Log.e("====", "==onClick===" + position);
//            }
//        });
//
//        return viewList.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewList.get(position%viewList.size()));
    }
}
